<?php

include 'common_script.php';

//CONFIG========================================================================
$LOCATION = 'C:\wamp\www\Supplier\Supplier\application\model\object\\';
$HOST_NAME = "localhost:81";
$DATABASE_NAME = "rz_supplier";
$USERNAME = "root";
$PASSWORD = "123456";
$INHERITANCE ="extends RTO";
$CLASS_PREFIX = "";
$CLASS_SUFFIX = "TO";
//==============================================================================

$queryShowTable = "SHOW TABLES";

if (!$link = mysql_connect($HOST_NAME, $USERNAME, $PASSWORD)) {
    echo 'Could not connect to mysql';
    exit;
}
if (!mysql_select_db($DATABASE_NAME, $link)) {
    echo 'Could not select database';
    exit;
}
$result = mysql_query($queryShowTable, $link);
if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_array($result)) {
    $table = $row[0];
    $queryShowColumn = "SHOW COLUMNS FROM `".$table."`";
    $className = convertClass($table);
    $properties = "";
    $functions = "";
    $arr_keys_string = "";
    $result2 = mysql_query($queryShowColumn, $link);
    if (!$result2) {
        echo "DB Error, could not query the database\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }
    
    while ($row2 = mysql_fetch_array($result2)) {
        
        $pro = $row2[0];
        $properties.="public $".$pro.";";
        
        
        $properties.="\n";
        $properties.="\t\t\t";
        
        if($row2[3]=="PRI"){
            if($arr_keys_string != ""){
                $arr_keys_string.=",";
            }
            $arr_keys_string.= "\"".$pro."\" => $"."this->".$pro;
        }
        
        
        if($row2[5]=="auto_increment"){
            $functions.="public function  getKeyAI(){
                           return \"$pro\";
                        }";
        }
        
        
    }
    
    $functions.="\n\n";
    $functions.="\t\t\t";
    
    $functions.="public function getPrimaryKey(){
                    return array($arr_keys_string);
                }";
    
        $functions.="\n\n";
        $functions.="\t\t\t";
    
    $functions.="public function getName(){
                    return \"".  ($table)."\";
                }";
                
        $functions.="\n\n";
        $functions.="\t\t\t";
    
    $functions.="public function getNamespace(){
                    return \"".($DATABASE_NAME)."\";
                }";
	
	        $functions.="\n\n";
        $functions.="\t\t\t";
    
    $functions.="public static function newRTO(){
                    return new $CLASS_PREFIX".$className."$CLASS_SUFFIX();
                }";
    
    $template = "<?php
        class $CLASS_PREFIX".$className."$CLASS_SUFFIX $INHERITANCE{ \n\n        
            $properties
            $functions
        }
    ";
    
    file_put_contents($LOCATION.$className."$CLASS_SUFFIX.class.php", $template);

    echo "Created class: $className"."$CLASS_SUFFIX \n";
 
}



//*************
